package com.cloud.wms.ui.main.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.utils.NumberUtils;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.config.TaskCode;
import com.cloud.wms.ui.group.collect.GroupCollectActivity;
import com.cloud.wms.ui.group.obtain.ObtainScanContainerActivity;
import com.cloud.wms.ui.group.sampling.SamplingOrderActivity;
import com.cloud.wms.ui.in.activity.InOrderDetailActivity;
import com.cloud.wms.ui.in.inventory.InTypeActivity;
import com.cloud.wms.ui.in.register.InRegisterActivity;
import com.cloud.wms.ui.main.contract.MainContract;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.main.presnter.MainPresenter;
import com.cloud.wms.ui.sort.BatchActivity;
import com.cloud.wms.ui.sort.car.CarTypeActivity;
import com.cloud.wms.ui.sort.group.GroupBindActivity;
import com.cloud.wms.ui.sort.order.OrderBindActivity;
import com.cloud.wms.ui.sort.sku.SkuBindActivity;
import com.cloud.wms.widget.dialog.AlertDialogBuilder;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-20 15:22
 * @Description: 波次单列表
 */
public class TaskSearchActivity extends BaseBarListActivity<TaskEntity, MainPresenter> implements MainContract.View {
    @BindView(R.id.iv_search)
    ImageButton ivSearch;
    @BindView(R.id.scan_text)
    ScanText scanText;
    private boolean isWaitTask;
    private String taskCode;

    @Override
    protected String barTitle() {
        isWaitTask = getIntent().getBooleanExtra(IntentKey.TYPE_KEY, true);
        return isWaitTask ? "待办任务查询" : "我的任务查询";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_main_task_search;
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_main_task;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        taskCode = getIntent().getStringExtra(IntentKey.TASK_KEY);
        initScanText(scanText);
        mAdapter.addChildClickViewIds(R.id.tv_task_detail, R.id.btn_task_refuse, R.id.btn_task_receive, R.id.btn_task_execute);

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            TaskEntity taskEntity = (TaskEntity) adapter.getData().get(position);
            switch (view.getId()) {
                case R.id.tv_task_detail:
                    switch (taskEntity.getTaskCode()) {
                        case TaskCode.CODE_001001://登记
                        case TaskCode.CODE_001002://清点
                        case TaskCode.CODE_001003://上架
                            InOrderDetailActivity.start(TaskSearchActivity.this, taskEntity);
                            break;
                        case TaskCode.CODE_002002://团购备货
                        case TaskCode.CODE_002003://团购集货
                        case TaskCode.CODE_002004://团购绑定流水线
                        case TaskCode.CODE_002005://团购抽检
                        case TaskCode.CODE_003001://1:散单 2:单SKU 3:笼车
                            BatchActivity.start(TaskSearchActivity.this, taskEntity);
                            break;
                    }
                    break;
                case R.id.btn_task_refuse:
                    mPresenter.refuseTask(taskEntity);
                    break;
                case R.id.btn_task_execute:
                    // 订单拣选
                    if (taskEntity.isOrderSort())
                        mPresenter.executeOrderTask(taskEntity);
                    else
                        mPresenter.executeTask(taskEntity);
                    break;
                case R.id.btn_task_receive:
                    new AlertDialogBuilder(TaskSearchActivity.this)
                            .setTitle("提示")
                            .setMessage("是否领取该任务？")
                            .setNegativeButton("确定", (dialog, which) -> {
                                // 订单拣选
                                if (taskEntity.isOrderSort())
                                    mPresenter.receiveOrderTask(taskEntity, false);
                                else
                                    mPresenter.receiveTask(taskEntity, false);
                            })
                            .setPositiveButton("确定并执行", (dialog, which) -> {
                                if (taskEntity.isOrderSort())
                                    mPresenter.receiveOrderTask(taskEntity, true);
                                else
                                    mPresenter.receiveTask(taskEntity, true);
                            })
                            .setNeutralButton("取消", null)
                            .show();
                    break;
            }
        });
    }

    @Override
    public void onReload() {
        super.onReload();
    }

    @Override
    protected boolean isRefresh() {
        return true;
    }

    @Override
    protected boolean isLoadMore() {
        return false;
    }

    @Override
    protected void loadListData(int page) {
        super.loadListData(page);
        mPresenter.searchTask(taskCode, scanText.getText(), isWaitTask);
    }

    @Override
    protected void onScanHandle(String result, int resultCode) {
        super.onScanHandle(result, resultCode);
        mPresenter.searchTask(taskCode, result, isWaitTask);
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, TaskEntity item) {
        helper.setText(R.id.tv_task_code, item.getTaskCode())
                .setText(R.id.tv_task_key, item.getTaskShortName())
                .setText(R.id.tv_task_value, item.getBillCode())
                .setText(R.id.tv_task_create_time, item.getCreateDate())
                .setGone(R.id.view_line, helper.getAdapterPosition() != 0)
                .setGone(R.id.btn_task_execute, isWaitTask)
                .setGone(R.id.btn_task_receive, !isWaitTask)
                .setVisible(R.id.btn_task_refuse, !isWaitTask);
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<TaskEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case MainPresenter.TASK_REFUSE:
                success("任务拒绝成功！");
                mPresenter.loadMineTask(taskCode);
                break;
            case MainPresenter.TASK_RECEIVE:
                mPresenter.loadWaitTask(taskCode);
                success("任务领取成功！");
                break;
            case MainPresenter.TASK_EXECUTE:
                TaskEntity taskEntity = (TaskEntity) message.obj;
                switch (taskEntity.getTaskCode()) {
                    case TaskCode
                            .CODE_001001://登记
                        InRegisterActivity.start(TaskSearchActivity.this, taskEntity, null);
                        break;
                    case TaskCode
                            .CODE_001002://清点
                        InTypeActivity.start(TaskSearchActivity.this, taskEntity, null);
                        break;
                    case TaskCode
                            .CODE_001003://上架
                        com.cloud.wms.ui.in.shelf.InTypeActivity.start(TaskSearchActivity.this, taskEntity, null);
                        break;
                    case TaskCode
                            .CODE_002002://团购备货
                        GroupBindActivity.start(TaskSearchActivity.this, false, taskEntity);
                        break;
                    case TaskCode
                            .CODE_002003://团购集货
                        GroupCollectActivity.start(TaskSearchActivity.this, taskEntity.getMainBillID() + "", taskEntity);
                        break;
                    case TaskCode
                            .CODE_002004://团购绑定流水线
                        ObtainScanContainerActivity.start(TaskSearchActivity.this, taskEntity, taskEntity.getMainBillID());
                        break;
                    case TaskCode
                            .CODE_002005://团购抽检
                        SamplingOrderActivity.start(TaskSearchActivity.this, taskEntity, null);
                        break;
                    case TaskCode.CODE_003001://1:散单 2:单SKU 3:笼车
                        switch (taskEntity.getWaveType()) {
                            case 1:
                                OrderBindActivity.start(TaskSearchActivity.this, taskEntity, false);
                                break;
                            case 2:
                                SkuBindActivity.start(TaskSearchActivity.this, taskEntity, false);
                                break;
                            case 3:
                                CarTypeActivity.start(TaskSearchActivity.this, taskEntity);
                                break;
                        }
                        break;
                }
                break;
        }
    }

    @OnClick({R.id.iv_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
                if (TextUtils.isEmpty(scanText.getText())) {
                    info("查询关键词不能为空！");
                    return;
                }
                mPresenter.searchTask(taskCode, scanText.getText(), isWaitTask);
                break;
        }
    }

    public static void start(Context context, String taskCode, boolean isWaitTask) {
        Intent intent = new Intent(context, TaskSearchActivity.class);
        intent.putExtra(IntentKey.TASK_KEY, taskCode);
        intent.putExtra(IntentKey.TYPE_KEY, isWaitTask);
        context.startActivity(intent);
    }
}
